home *** CD-ROM | disk | FTP | other *** search
/ Java Interactive Reference Guide / Java Interactive Reference Guide.iso / autorun / source.dir / 00044_15.txt < prev    next >
Encoding:
Text File  |  1980-01-11  |  9.6 KB  |  313 lines

  1. /* ----------------------------------------------------------------
  2.  * Clock 1.0, Copyright (c) 1995 Nils Hedstr├╢m, All Rights Reserved.
  3.  * Permission to use, copy, modify, and distribute this software and its
  4.  * documentation for NON-COMMERCIAL purposes and without fee is hereby
  5.  * granted provided that this copyright notice and appropiate documention
  6.  * appears in all copies.
  7.  * 
  8.  * NILS HEDSTR├ûM MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
  9.  * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT
  10.  * LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  11.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. NILS HEDSTR├ûM SHALL NOT BE LIABLE
  12.  * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  13.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  14.  * 
  15.  * For documentation look at http://www-und.ida.liu.se/~d94nilhe/java/applets.html */
  16.  
  17. import browser.Applet;
  18. import awt.Graphics;
  19. import awt.WServer;
  20. import awt.Color;
  21. import awt.Image;
  22. import java.util.Date;
  23. import java.lang.Math;
  24. import net.www.html.URL;
  25.  
  26. class Clock extends Applet implements Runnable
  27. {
  28.       int width,height,num_lines,hour,minute,second,sleep,hour_width,minute_width,second_width;
  29.     int hour_type,minute_type,second_type;
  30.     double pi=3.1415926535,hour_len,minute_len,second_len;
  31.     Color hour_col,minute_col,second_col,border_col,background_col;
  32.     URL homepage=new URL("http://www-und.ida.liu.se/~d94nilhe/java/applets.html");
  33.     Thread animate=null;
  34.     Image im;
  35.     Graphics offscreen;
  36.     public void init() 
  37.       {
  38.         width=getIntAttribute("width",0,200,1000);
  39.         height=getIntAttribute("height",0,200,1000);
  40.         num_lines=getIntAttribute("num_lines",3,12,1000);
  41.         sleep=getIntAttribute("sleep",5,1000,20000);
  42.         hour_len=getIntAttribute("hour_len",0,60,100)/200.;
  43.         minute_len=getIntAttribute("minute_len",0,75,100)/200.;
  44.         second_len=getIntAttribute("second_len",0,90,100)/200.;
  45.         hour_width=getIntAttribute("hour_width",0,7,100);
  46.         minute_width=getIntAttribute("minute_width",0,5,100);
  47.         second_width=getIntAttribute("second_width",0,3,100);
  48.         hour_type=getIntAttribute("hour_type",0,3,3);
  49.         minute_type=getIntAttribute("minute_type",0,3,3);
  50.         second_type=getIntAttribute("second_type",0,3,3);
  51.         hour_col=StrHexToColor(getStringAttribute("hour_col","ff0000"));
  52.         minute_col=StrHexToColor(getStringAttribute("minute_col","00ff00"));
  53.         second_col=StrHexToColor(getStringAttribute("second_col","0000ff"));
  54.         border_col=StrHexToColor(getStringAttribute("border_col","000000"));
  55.         background_col=StrHexToColor(getStringAttribute("background_col","c0c0c0"));
  56.         
  57.         resize(width,height);
  58.         try 
  59.           {
  60.         im = createImage(width, height);
  61.         offscreen = new Graphics(im);
  62.           } 
  63.         catch (Exception e) 
  64.           {
  65.         // double-buffering not available
  66.           offscreen = null;
  67.           }
  68.       }
  69.     public void paintApplet(Graphics g) 
  70.       {
  71.         int x1,x2,x3,y1,y2,y3;
  72.         x1=(int)(second_len*width*Math.cos(2.*pi*second/60.)+width/2.);
  73.         y1=(int)(second_len*height*Math.sin(2.*pi*second/60.)+height/2.);
  74.         x2=(int)(minute_len*width*Math.cos(2.*pi*minute/60.)+width/2.);
  75.         y2=(int)(minute_len*height*Math.sin(2.*pi*minute/60.)+height/2.);
  76.         x3=(int)(hour_len*width*Math.cos(2.*pi*(hour/12.+(minute+15)/720.))+width/2.);
  77.         y3=(int)(hour_len*height*Math.sin(2.*pi*(hour/12.+(minute+15)/720.))+height/2.);
  78.         g.SetColor(background_col);
  79.         g.fillRect(0, 0, width, height); 
  80.         circle(g,width/2,height/2,width/2,height/2,num_lines,border_col);
  81.         g.SetColor(hour_col);
  82.         drawArm(g,(int)(width/2.),(int)(height/2.),x3,y3,hour_width,hour_type);
  83.         g.SetColor(minute_col);
  84.         drawArm(g,(int)(width/2.),(int)(height/2.),x2,y2,minute_width,minute_type);
  85.         g.SetColor(second_col);
  86.         drawArm(g,(int)(width/2.),(int)(height/2.),x1,y1,second_width,second_type);
  87.       }
  88.     
  89.     
  90.         void DrawPoly(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4,
  91.               Graphics g) 
  92.       {
  93.     
  94.         int x, mny, mxy, mnx, mxx, yc;
  95.         int mul1, div1, mul2, div2, mul3, div3, mul4, div4;
  96.         
  97.         
  98.         // find the maximum y (mny) and minimum y (mny)
  99.        
  100.         mxy=Math.max(Math.max(y1,y2),Math.max(y3,y4));
  101.         mny=Math.min(Math.min(y1,y2),Math.min(y3,y4));
  102.         
  103.         
  104.         // constants needed for intersection calculations
  105.           mul1 = x1-x4;  div1 = y1-y4;
  106.         mul2 = x2-x1;  div2 = y2-y1;
  107.         mul3 = x3-x2;  div3 = y3-y2;
  108.         mul4 = x4-x3;  div4 = y4-y3;
  109.         
  110.         for (yc=mny; yc<mxy; yc++) {
  111.       mnx = width;
  112.       mxx =  -1;
  113.       
  114.       if ((y4 >= yc) || (y1 >= yc))
  115.         if ((y4 <= yc) || (y1 <= yc))
  116.           if (y4 != y1) {
  117.         x = ((yc-y4) * mul1 / div1) + x4;
  118.         if (x<mnx) mnx = x;
  119.         if (x>mxx) mxx = x;
  120.           }
  121.       
  122.       if ((y1 >= yc) || (y2 >= yc))
  123.       if ((y1 <= yc) || (y2 <= yc))
  124.         if (y1 != y2) {
  125.           x = ((yc-y1) * mul2 / div2) + x1;
  126.           if (x<mnx) mnx = x;
  127.           if (x>mxx) mxx = x;
  128.         }
  129.       
  130.       if ((y2 >= yc) || (y3 >= yc))
  131.         if ((y2 <= yc) || (y3 <= yc))
  132.           if (y2 != y3) {
  133.         x = ((yc-y2) * mul3 / div3) + x2;
  134.         if (x<mnx) mnx = x;
  135.         if (x>mxx) mxx = x;
  136.           }
  137.       
  138.       if ((y3 >= yc) || (y4 >= yc))
  139.         if ((y3 <= yc) || (y4 <= yc))
  140.           if (y3 != y4) {
  141.         x = ((yc-y3) * mul4 / div4) + x3;
  142.         if (x<mnx) mnx = x;
  143.         if (x>mxx) mxx = x;
  144.           }
  145.       
  146.       
  147.       
  148.       if (mnx<=mxx)
  149.         // draw the horizontal line
  150.           g.drawLine(mnx,yc,mxx,yc);
  151.       
  152.     }
  153.       }
  154.     public void drawArm(Graphics g, int x1,int y1, int x2,int y2, int arm_width, int arm_type)
  155.       {
  156.         int dx,dy;
  157.         double len,add,n;
  158.         len=Math.sqrt(Math.pow(x2-x1,2.)+Math.pow(y2-y1,2.));
  159.         dx=(int)(arm_width*(y2-y1)/len);
  160.         dy=(int)(arm_width*(x1-x2)/len);
  161.         if(arm_width==1) g.drawLine(x1,y1,x2,y2);
  162.         else
  163.           {
  164.         switch (arm_type)
  165.           {
  166.           case 0:
  167.             break;
  168.           case 1:
  169.             DrawPoly(x1-dx,y1-dy,x1+dx,y1+dy,x2,y2,x2,y2,g);
  170.             break;
  171.           case 2:
  172.             DrawPoly(x1-dx,y1-dy,x1+dx,y1+dy,x2+dx,y2+dy,x2-dx,y2-dy,g);
  173.             break;
  174.           case 3:
  175.             DrawPoly(x1,y1,(x1+x2)/2+dx,(y1+y2)/2+dy,x2,y2,(x1+x2)/2-dx,(y1+y2)/2-dy,g);
  176.           }
  177.           }
  178.           }
  179.     public void paint(Graphics g) 
  180.       {
  181.         if (offscreen != null) 
  182.           { 
  183.         // double-buffering available
  184.           paintApplet(offscreen);
  185.         g.drawImage(im, 0, 0);
  186.         } else 
  187.           {
  188.         // no double-buffering
  189.           paintApplet(g);
  190.           }
  191.       }
  192.     public void run() {
  193.       while (true) 
  194.         {
  195.           Date today= new Date();
  196.           hour=today.getHours()%12-3;
  197.           minute=today.getMinutes()-15;
  198.           second=today.getSeconds()-15;
  199.           repaint();
  200.           Thread.sleep(sleep);
  201.         }    
  202.     }
  203.     public void mouseEnter()
  204.       {
  205.         showStatus("Who made this clock?");
  206.       }
  207.     public void mouseExit()
  208.       {
  209.         showStatus("");
  210.       }
  211.     
  212.     public void mouseDown(int x,int y)
  213.       {
  214.         /*if(x>=0 && x<width && y>=0 && y<height)*/
  215.           showDocument(homepage);
  216.       }
  217.     public void circle(Graphics g,int x, int y, int xr,int yr, int num,Color col)
  218.         {
  219.           double add,count;
  220.           add=2.*pi/num;
  221.           g.SetColor(col);
  222.           for(count=0;count<=2.*pi;count+=add)
  223.           {
  224.         g.drawLine(x+(int)(xr*Math.sin(count)),
  225.                y+(int)(yr*Math.cos(count)),
  226.                x+(int)(xr*Math.sin(count+add)),
  227.                y+(int)(yr*Math.cos(count+add)));
  228.           }
  229.       }
  230.     public void start() {
  231.       if (animate == null) {
  232.         animate = new Thread(this);
  233.         animate.start();
  234.       }
  235.     }
  236.     
  237.     public void stop() {
  238.       if (animate != null) {
  239.         animate.stop();
  240.         animate=null;
  241.       }
  242.     }
  243.     public void update(Graphics g) 
  244.       {
  245.         paint(g);
  246.       }
  247.  
  248.         public int charHexToInt(char in)
  249.       {
  250.             int dec;
  251.             if (in>='0' && in<='9') return (int)(in-'0');
  252.         if (in>='A' && in<='F') return (int)(in-'A'+10);
  253.         if (in>='a' && in<='f') return (int)(in-'a'+10);
  254.         return -1;
  255.       }
  256.     public Color StrHexToColor(String in)
  257.       {
  258.         Color col;
  259.         int r,g,b;
  260.         r=StrHexToInt(in.substring(0,2));
  261.         g=StrHexToInt(in.substring(2,4));
  262.         b=StrHexToInt(in.substring(4,6));
  263.         if(r>=0 && g>=0 && b>=0) return new Color(r,g,b);
  264.         else
  265.         return Color.red;
  266.       }
  267.         
  268.         public int StrHexToInt(String in)
  269.       {
  270.         int lown, highn;
  271.         highn = charHexToInt(in.charAt(0)) * 16;
  272.             lown = charHexToInt(in.charAt(1));
  273.             if ((highn >= 0) && (lown >= 0))
  274.               return (highn+lown);
  275.             else
  276.               return -1;
  277.       }
  278.               
  279.  
  280.  
  281.  
  282.     public int getIntAttribute (String att, int min, int def, int max)
  283.       {
  284.         try {
  285.           int ret = Integer.parseInt(getAttribute(att));
  286.           if (ret < min)
  287.         return min;
  288.           if (ret > max)
  289.         return max;
  290.           return ret;
  291.         } catch(Exception e) {
  292.           return def;
  293.         }
  294.         
  295.       } 
  296.     public String getStringAttribute (String att, String def)
  297.       {
  298.         String ret;
  299.         
  300.         try {
  301.           ret = getAttribute(att);
  302.           if (ret.length() < 1)
  303.         return def;
  304.           return ret;
  305.         } catch(Exception e) {
  306.           return def;
  307.         }
  308.         
  309.       } 
  310.       }
  311.  
  312.  
  313.